"use strict";
cc._RF.push(module, '1fefcsFcIlHZbXUWnHH/0ns', 'MainGame');
// scripts/huochai/MainGame.js

'use strict';

var HuoChaiTypes = require('HuoChaiTypes');
var gamelevels = HuoChaiTypes.gamelevels;
var gamelevel = 0; //真实显示关卡数
var havegamelevel = 163; //有的管卡数关卡数
var falsegamelevel = gamelevel % havegamelevel; //根据有限关卡设置成无限循环
//var MainGame =
cc.Class({
    extends: cc.Component,

    properties: {
        TxtGuanshu: cc.RichText,
        TxtTiShi: cc.RichText,
        btnDashangTu: cc.Node,
        shuzi0: cc.Node,
        shuzi1: cc.Node,
        shuzi2: cc.Node,
        shuzi3: cc.Node
    },

    // use this for initialization
    onLoad: function onLoad() {
        //MainGame.instance = this;
        this.listenerStorageSyncGet();
        this.restartGame();
        cc.log('Next scene preloaded');
    },
    //清空界面 准备开始，或者重新开始
    restartGame: function restartGame() {
        falsegamelevel = gamelevel % havegamelevel;
        this.TxtGuanshu.string = "<color=#ffffff>第</c><color=#0fffff>" + (gamelevel + 1) + "</color><color=#ffffff>关</c>";
        this.TxtTiShi.string = "";
        this.btnDashangTu.active = false;
        this.initGame(gamelevels[falsegamelevel]);
    },

    //初始化数字
    initGame: function initGame(levels) {

        this.shuzi0 = this.shuzi0.getComponent('ShuZhi');
        this.shuzi0.init(levels[0]);
        this.shuzi1 = this.shuzi1.getComponent('ShuZhi');
        this.shuzi1.init(levels[1]);
        this.shuzi2 = this.shuzi2.getComponent('ShuZhi');
        this.shuzi2.init(parseInt(levels[2] / 10));
        this.shuzi3 = this.shuzi3.getComponent('ShuZhi');
        this.shuzi3.init(levels[2] % 10);
    },

    update: function update() {
        //console.log('this.shuzi0.getNum()============' + this.shuzi0.getNum());
        //符合正确答案a+b=c
        //console.log("falsegamelevel================" + falsegamelevel);
        if (this.shuzi0.getNum() == gamelevels[falsegamelevel][3] && this.shuzi1.getNum() == gamelevels[falsegamelevel][4] && this.shuzi2.getNum() * 10 + this.shuzi3.getNum() == gamelevels[falsegamelevel][5]) {
            gamelevel++;
            this.listenerStorageSyncSave();
            this.restartGame();
        }
        //符合正确答案加法交换律 b+a=c
        else if (this.shuzi0.getNum() == gamelevels[falsegamelevel][4] && this.shuzi1.getNum() == gamelevels[falsegamelevel][3] && this.shuzi2.getNum() * 10 + this.shuzi3.getNum() == gamelevels[falsegamelevel][5]) {
                gamelevel++;
                this.listenerStorageSyncSave();
                this.restartGame();
            }
            //正确答案也猜不到的答案 等式只要成立就好
            else if (this.shuzi0.getNum() + this.shuzi1.getNum() == this.shuzi2.getNum() * 10 + this.shuzi3.getNum()) {
                    gamelevel++;
                    this.listenerStorageSyncSave();
                    this.restartGame();
                } else if (this.shuzi0.getNum() == -1 || this.shuzi1.getNum() == -1 || this.shuzi2.getNum() == -1 || this.shuzi3.getNum() == -1) {
                    this.initGame(gamelevels[falsegamelevel]);
                } else if (this.shuzi0.getNum() != gamelevels[falsegamelevel][0] || this.shuzi1.getNum() != gamelevels[falsegamelevel][1] || this.shuzi2.getNum() * 10 + this.shuzi3.getNum() != gamelevels[falsegamelevel][2]) {
                    this.initGame(gamelevels[falsegamelevel]);
                }
    },

    clickbtnBack: function clickbtnBack() {
        cc.director.loadScene('mainmenmu');
    },
    clickbtnTishi: function clickbtnTishi() {
        this.TxtTiShi.string = "<color=#ffffff>" + gamelevels[falsegamelevel][3] + "</c><color=#0fffff>+</color><color=#ffffff>" + gamelevels[falsegamelevel][4] + "</c><color=#0fffff>=</color><color=#ffffff>" + gamelevels[falsegamelevel][5] + "</c>";
    },
    clickbtnDashang: function clickbtnDashang() {
        this.btnDashangTu.active = !this.btnDashangTu.active;
    },
    clickbtnQiuzhu: function clickbtnQiuzhu() {
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            wx.shareAppMessage({
                title: '我要主动拉起分享啦,会不会来玩玩啊，好玩得很！',
                imageUrl: canvas.toTempFilePathSync({
                    destWidth: 500,
                    destHeight: 400
                }),
                success: function success(shareTickets, groupMsgInfos) {
                    console.log('主动成功');
                },
                fail: function fail(res) {
                    console.log('主动失败');console.log(res);
                }
            });
        }
    },

    /**
    * 数据同步存储
    */
    listenerStorageSyncSave: function listenerStorageSyncSave() {
        console.log('数据同步存储');
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            wx.setStorageSync('gamelevel', gamelevel);
            this.setUserCloudStorage();
            //this.getFriendCloudStorage();
        }
    },

    /**
     * 数据同步获取
     */
    listenerStorageSyncGet: function listenerStorageSyncGet() {
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            // var that = this;
            console.log('数据同步获取');
            var value = wx.getStorageSync(HuoChaiTypes.gamelevel, 0);
            gamelevel = value;

            if (gamelevel == null) {
                gamelevel = 0;
            }
            console.log('数据同步获取gamelevel=====' + gamelevel);
        }
    },

    /**
    * 数据同步存储
    */
    setUserCloudStorage: function setUserCloudStorage() {
        console.log('(gamelevel+1).toString()=====' + (gamelevel + 1).toString());
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            wx.setUserCloudStorage({

                KVDataList: [{ key: '1', value: (gamelevel + 1).toString() }, { key: '2', value: '3' }],
                success: function success(res) {
                    console.log('setUserCloudStorage', 'success', res);
                },
                fail: function fail(res) {
                    console.log('setUserCloudStorage', 'fail');
                },
                complete: function complete(res) {
                    console.log('setUserCloudStorage', 'ok');
                }
            });
        }
    },

    /**
     * 数据同步获取
     */
    getFriendCloudStorage: function getFriendCloudStorage() {
        wx.getFriendCloudStorage({
            keyList: ["1"],
            success: function success(res) {
                //let data = res.data
                console.log("getFriendCloudStorage:", res);
            },
            fail: function fail(res) {
                console.log("getFriendCloudStorage fail:", res);
            }
        });
    }

});

cc._RF.pop();